import scrapy
from taobao_spider.items import TaobaoSpiderItem


class TaobaoSpider(scrapy.Spider):
    name = "taobao"
    allowed_domains = ["taobao.com"]
    # 这里一顿捣鼓的操作 就是直接操作第一页
    start_urls = ['https://login.taobao.com/member/login.jhtml']
    # https://s.taobao.com/search?page=1&q=%E6%89%8B%E6%9C%BA&tab=all
    def parse(self, response):
        print('当前采集页面：', response.url)
        search_condition = getattr(self,'search_condition')
        print('code编码：',search_condition)
        datas = response.xpath('//*[@id="pageContent"]/div[1]/div[3]/div[3]/div/div')
        for data in datas:
            item = TaobaoSpiderItem()
            item['shop_name'] = data.xpath('./a/div/div[1]/div[2]/div/span/text()[1]').extract_first()
            item['pay_people'] = data.xpath('./a/div/div[1]/div[4]/span[2]/text()').extract_first()
            item['title'] = data.xpath('./a/div/div[3]/div[1]/a/text()').extract_first()
            item['price'] = data.xpath('./a/div/div[1]/div[4]/div[1]/span[1]/text()').extract_first()
            yield item
            # 这里算是从第二页开始
        for page in range(2,8):
            print(f'当前从第{page}页开始爬了')
            new_url =f'https://s.taobao.com/search?commend=all&ie=utf8&initiative_id=tbindexz_20170306&page={page}&q={search_condition}&search_type=item&sourceId=tb.index&spm=a21bo.jianhua%2Fa.201856.d13&ssid=s5-e&tab=all'
            yield scrapy.Request(url=new_url, callback=self.parse)
